import LogAPI from "@/api/system/log-api";
import UserAPI from "@/api/system/user-api";
import { successMap } from "@/commonData/dict";
import { useTableData } from "@/components/SmTable/src/utils";

export const useTable = () => {
  const list: any = ref([]);
  const total = ref(0);
  const loading = ref(false);

  const { tableOptions, transformTableOptions } = useTableData();

  const fieldMap = {
    module: "模块名称",
    operation: "操作类型",
    method: "请求方法",
    path: "请求路径",
    params: "请求参数",
    ip: "IP地址",
    location: "IP位置",
    browser: "浏览器",
    os: "操作系统",
    duration: "执行时间(ms)",
    status: {
      label: "状态",
      textMap: successMap,
    },
    errorMsg: "错误信息",
  };
  tableOptions.value = transformTableOptions(fieldMap);
  // tableOptions.value[0].width = undefined;

  const queryParams: any = reactive({
    pageNum: 1,
    pageSize: 10,
  });

  /** 查询列表 */
  const getList = async () => {
    loading.value = true;
    try {
      const data = await LogAPI.getPage(queryParams);
      list.value = data.list;
      total.value = data.total;
    } finally {
      loading.value = false;
    }
  };

  function handleQuery() {
    queryParams.pageNum = 1;
    getList();
  }

  const queryFormRef = ref();
  /** 添加/修改操作 */
  const formRef = ref();
  const openForm = (type: string, id?: number) => {
    formRef.value.open(type, id);
  };

  /** 重置按钮操作 */
  const handleResetQuery = () => {
    queryFormRef.value.resetFields();
    nextTick(() => {
      handleQuery();
    });
  };

  return {
    tableOptions,
    list,
    total,
    loading,
    queryParams,
    getList,
    handleQuery,
    handleResetQuery,
    queryFormRef,
    formRef,
    openForm,
  };
};
